package com.medicine.hbp.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.ViewPagerAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.sp.UtilsSp;

import java.util.ArrayList;
import java.util.List;

public class AndyViewPagerActivity extends BaseActivity implements
		OnClickListener, OnPageChangeListener {

	private ViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;
	private ImageView img01, img02, img03, img04;
	private Button btn02;
	private LinearLayout layout_img;

	private static final int[] pics = { R.mipmap.guide_1, R.mipmap.guide_2,
			R.mipmap.guide_3, R.mipmap.guide_4 };
	private ImageView[] dots;
	private int currentIndex;

	private Handler handler2 = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Intent intent = new Intent();
			intent.setClass(AndyViewPagerActivity.this, MainActivity.class);
			AndyViewPagerActivity.this.startActivity(intent);
			UtilsSp.setIsOne(true, AndyViewPagerActivity.this);
			finish();
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// ȥ����Ϣ��
		setContentView(R.layout.activity_andy_view_pager);
		btn02 = (Button) findViewById(R.id.btn02);
		img01 = (ImageView) findViewById(R.id.img01);
		img02 = (ImageView) findViewById(R.id.img02);
		img03 = (ImageView) findViewById(R.id.img03);
		img04 = (ImageView) findViewById(R.id.img04);

		layout_img = (LinearLayout) findViewById(R.id.layout_img);

		views = new ArrayList<View>();

		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);

		// 初始化引导图片列�?
		for (int i = 0; i < pics.length; i++) {
			ImageView iv = new ImageView(this);
			iv.setLayoutParams(mParams);
			iv.setImageResource(pics[i]);
			views.add(iv);
		}
		vp = (ViewPager) findViewById(R.id.viewpager);
		vpAdapter = new ViewPagerAdapter(views);
		vp.setAdapter(vpAdapter);
		vp.setOnPageChangeListener(this);

		btn02.setVisibility(View.GONE);
		// button = (ImageView) findViewById(R.id.button);
		// btn01 = (Button) findViewById(R.id.btn01);
		// btn02 = (Button) findViewById(R.id.btn02);
		// layout_loginandzhuce= (LinearLayout)
		// findViewById(R.id.layout_loginandzhuce);
		// button.setVisibility(View.GONE);
		// initDots();
		// button.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// Intent intent=new Intent();
		// intent.setClass(AndyViewPagerActivity.this, MainActivity.class);
		// AndyViewPagerActivity.this.startActivity(intent);
		// UtilsSp.setIsOne(true,AndyViewPagerActivity.this);
		// finish();
		// }
		// });
		// btn01.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// StartIntent.startLogin(AndyViewPagerActivity.this);
		// UtilsSp.setIsOne(true,AndyViewPagerActivity.this);
		// finish();
		// }
		// });
		btn02.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Intent intent = new Intent(aty, RegisterActivity.class);
				// AndyViewPagerActivity.this.startActivity(intent);
				// UtilsSp.setIsOne(true,AndyViewPagerActivity.this);
				// finish();

				Intent intent = new Intent();
				intent.setClass(AndyViewPagerActivity.this, MainActivity.class);
				AndyViewPagerActivity.this.startActivity(intent);
				UtilsSp.setIsOne(true, AndyViewPagerActivity.this);
				finish();
			}
		});

	}

	// private void initDots() {
	// // LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
	//
	// dots = new ImageView[pics.length];
	//
	// for (int i = 0; i < pics.length; i++) {
	// dots[i] = (ImageView) ll.getChildAt(i);
	// dots[i].setEnabled(true);//都设为灰�?
	// dots[i].setOnClickListener(this);
	// dots[i].setTag(i);//设置位置tag，方便取出与当前位置对应
	// }
	//
	// currentIndex = 0;
	// dots[currentIndex].setEnabled(false);//设置为白色，即�?中状�?
	// }
	//
	// /**
	// *设置当前的引导页
	// */
	private void setCurView(int position) {
		if (position < 0 || position >= pics.length) {
			return;
		}

		vp.setCurrentItem(position);
	}

	private void setCurDot(int positon) {
		if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
			return;
		}

		dots[positon].setEnabled(false);
		dots[currentIndex].setEnabled(true);

		currentIndex = positon;
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		img01.setImageResource(R.drawable.back_image_splash01);
		img02.setImageResource(R.drawable.back_image_splash01);
		img03.setImageResource(R.drawable.back_image_splash01);
		img04.setImageResource(R.drawable.back_image_splash01);
		switch (arg0) {
		case 0:
			img01.setImageResource(R.drawable.back_image_splash02);
			break;
		case 1:
			img02.setImageResource(R.drawable.back_image_splash02);
			break;
		case 2:
			img03.setImageResource(R.drawable.back_image_splash02);
			break;
		case 3:
			img04.setImageResource(R.drawable.back_image_splash02);
			break;
		default:
			break;
		}
		if (arg0 == 3) {
			layout_img.setVisibility(View.GONE);
			btn02.setVisibility(View.VISIBLE);
		} else {
			layout_img.setVisibility(View.VISIBLE);
			btn02.setVisibility(View.GONE);
		}

		// if(arg0 == 0){
		// layout_loginandzhuce.setVisibility(View.VISIBLE);
		// button.setVisibility(View.GONE);
		// } else if(arg0 == 3){
		// layout_loginandzhuce.setVisibility(View.GONE);
		// button.setVisibility(View.VISIBLE);
		// }else{
		// layout_loginandzhuce.setVisibility(View.GONE);
		// button.setVisibility(View.GONE);
		// }

	}

	@Override
	public void onClick(View v) {
		// int position = (Integer)v.getTag();
		// setCurView(position);
		// setCurDot(position);
	}

}